#region Using Statements
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace MyFirstXNAGame.Source.Sprites
{
    class Samus : Sprite
    {
        public float firingSpeed = .5f;
        public Boolean jumpPressed;
        public Samus()
        {
            init();
            this.velocity = new Vector2(0.0f, 0.0f);
            this.color = Color.White;
            this.bounds.X = 360;
            this.bounds.Y = 240;
            this.bounds.Width = 70;
            this.bounds.Height = 84;
            this.numFrames = 10;
            this.levelOrigin.X = 300;
            this.levelOrigin.Y = 300;
            acceleration.Y = 2500.0f; //gravity
        }
        public override bool update(float xOrigin, float yOrigin, int width, int height, GameTime gameTime, ArrayList environment, Vector2 theOrigin)
        {
            float xMin = theOrigin.X;
            float yMin = theOrigin.Y;
            animate(gameTime.TotalGameTime.Milliseconds);
            accelerate(gameTime);
            accelerateX = true;
            accelerateY = true;
            Sprite collisionSprite;
            Vector2 movement = velocity * (float)gameTime.ElapsedGameTime.TotalSeconds;
            updateX(xOrigin, width, gameTime, movement, xMin);
            collisionSprite = detectCollisions(environment);
            if (collisionSprite != null)
            {
                if (movement.X > 0)
                {
                    float overlap = (lastCollidedCollisionBox.X  + lastCollidedCollisionBox.Width - collisionSprite.lastCollidedCollisionBox.X);
                    levelOrigin.X = levelOrigin.X - overlap;
                    bounds.X = bounds.X - (int)overlap;
                }
                else if (movement.X < 0)
                {
                    float overlap = (collisionSprite.lastCollidedCollisionBox.X + collisionSprite.lastCollidedCollisionBox.Width - lastCollidedCollisionBox.X);                    
                    levelOrigin.X = levelOrigin.X + overlap;
                    bounds.X = bounds.X + (int)overlap;
                }
                velocity.X = 0;
            }
            updateY(yOrigin, width, gameTime, movement, yMin);
            collisionSprite = detectCollisions(environment);
            while (collisionSprite != null)
            {
                if (movement.Y > 0)
                {
                    levelOrigin.Y--;
                    bounds.Y--;
                    velocity.Y = 0;
                }
                else if (movement.Y < 0)
                {
                    levelOrigin.Y++;
                    bounds.Y++;
                    velocity.Y = .01f;
                }
                collisionSprite = detectCollisions(environment);
            }
            return true;
        }

        public override Rectangle getBounds()
        {
            bounds.Width = getAnimationFrame().Width;
            bounds.Height = getAnimationFrame().Height;
            return bounds;
        }
        public override void onCollission(Sprite other)
        {
            Console.WriteLine("COLLISSION!");
        }

        public override void input()
        {
            KeyboardState keyboard = Keyboard.GetState();
            keyboard = Keyboard.GetState();
            if(keyboard.IsKeyUp(Keys.Right) && velocity.X != 0)
            {
                velocity.X = 0f;
            }
            if (keyboard.IsKeyUp(Keys.Left) && velocity.X != 0)
            {
                velocity.X = 0f;
            }
            if (keyboard.IsKeyDown(Keys.Left))
            {
                velocity.X = -600.0f;
            }
            else if (keyboard.IsKeyDown(Keys.Right))
            {
                velocity.X = 600.0f;
            }
            if (keyboard.IsKeyDown(Keys.Space) && velocity.Y == 0f && jumpPressed == false)
            {
                velocity.Y = -1175f;
                jumpPressed = true;
            }
            else if (keyboard.IsKeyUp(Keys.Space))
            {
                jumpPressed = false;
                if (velocity.Y < 0)
                {
                    velocity.Y += 100f;
                }

            }

        }
        public override ArrayList getCollisionBox()
        {
            ArrayList result = new ArrayList();
            Rectangle box = new Rectangle((int)levelOrigin.X + 10, (int)levelOrigin.Y, bounds.Width-20, bounds.Height);
            result.Add(box);
            return result;
        }
    }
}
